/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import core.SellerCore;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.Dapversion;
import model.Department;
import model.Seller;

/**
 *
 * @author phinguyen
 */
public class ServletReportSeller extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        RequestDispatcher rd;
        HttpSession session = request.getSession();

        try {
            Seller sel = new Seller();
            sel = (Seller) session.getAttribute("user");
            System.out.println("seller----------------------------------------------------------------" + sel);
            SellerCore sellcore = new SellerCore();
            //récupérer identifiant du vendeur pour ici !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//            Integer sellerID = sellcore.session.getAttribute;
//            String sellerID=request.getParameter(null);
//            Seller seller = sellcore.getSellerbyID(sellerID);
            System.out.println("dddddddddddddddddddddddddddddddddddddddddddddddddd" + sellcore.dAPsInProgress(sellcore.getSellerByemail("phi@gmail.com")));
            System.out.println("dddddddddddddddddddddddddddddddddddddddddddddddddd" + sellcore.dAPsInProgress((Seller) sel));
            if (sel == sellcore.getSellerByemail("phi@gmail.com")) {
                System.out.println("true------------------------------------------------------------------------------------");
            }
            System.out.println("seller----------------------------------------------------------------" + sel + sellcore.getSellerByemail("phi@gmail.com"));
            List<Dapversion> inprogress = sellcore.dAPsInProgress(sel);
            System.out.println("inprogress--------------------------------" + inprogress.size());
            List<Dapversion> validated = sellcore.validatedDAPs(sel);
            System.out.println("validated----------------------------------" + validated.size());
            List<Department> dpt = new ArrayList<>();
            List<String> dates = new ArrayList<>();
            for (Dapversion dapversion : inprogress) {
                dpt.add(sellcore.getCurrentDept(dapversion));
                dates.add(sellcore.getLastModification(dapversion));
            }

            HashMap<Dapversion, String> hash = new HashMap<Dapversion, String>();
            int i;
            for (i = 0; i < inprogress.size(); i++) {
                hash.put(inprogress.get(i), dpt.get(i).getDeptname() + " (Last modification : " + dates.get(i) + ")");
                System.out.println(hash + "hash-----------------------------------------------------------------------------------------");
            }

            request.setAttribute("ListInProgress", inprogress);
            request.setAttribute("ListValidated", validated);
            System.out.println("passe par là !");
            request.setAttribute("hash", hash);
            rd = request.getRequestDispatcher("ReportingSeller.jsp");
            rd.forward(request, response);
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
